package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/9/20
 * Time: 18:24
 */
public class PrizePage extends Utils {
    public static String url = "http://101.42.27.228:8080/admin.html";

    public PrizePage() {
        super(url);
    }

    /**
     * 从后台管理页面点击活动列表页面，活动状态信息可以正常显示
     */
    public void checkPrizeList01() {
        locationSubpage("/html/body/div[2]/div[1]/ul/li[2]/ul/li[1]/a");
        // 检查奖品标题
        String title = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        // 检查奖品名
        String name = driver.findElement(By.xpath("//*[@id=\"prizeList\"]/tr[1]/td[3]")).getText();
        // 检查奖品描述
        String description = driver.findElement(By.xpath("//*[@id=\"prizeList\"]/tr[1]/td[4]")).getText();

        // 检测活动列表是否存在
        String actualText = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        String expectText = "奖品列表";
        assert expectText.equals(actualText);
        // 校验活动内容、活动标题、活动状态不为空
        assert !title.isEmpty();
        assert !name.isEmpty();
        assert !description.isEmpty();
    }

    /**
     * 新建奖品界面从后台是否可以打开
     */
    public void checkCreatePrize01() {
        locationSubpage("/html/body/div[2]/div[1]/ul/li[2]/ul/li[2]/a");
        // 检查新建奖品标题
        String title = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        // 检查奖品名称输入框
        driver.findElement(By.xpath("//*[@id=\"prizeName\"]"));
        //检查上传图片框
        driver.findElement(By.xpath("//*[@id=\"prizeImageUrl\"]"));
        // 检查奖品价格输入框
        driver.findElement(By.xpath("//*[@id=\"price\"]"));
        // 检查奖品描述输入框
        driver.findElement(By.xpath("//*[@id=\"description\"]"));
        // 检查创建奖品按钮
        String btn = driver.findElement(By.xpath("/html/body/div/button")).getText();

        String expectBtn = "创建奖品";
        String expectTitle = "开始创建奖品";

        assert title.equals(expectTitle);
        assert btn.equals(expectBtn);
    }

    /**
     * 都不输入
     */
    public void createPrizeFail01() {
        driver.findElement(By.xpath("/html/body/div/button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        assert actualMsg.contains("创建奖品失败");
    }
    /**
     * 仅输入奖品名称
     */
    public void createPrizeFail02() {
        driver.findElement(By.xpath("//*[@id=\"prizeName\"]")).sendKeys("自动化测试奖品");
        driver.findElement(By.xpath("/html/body/div/button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        assert actualMsg.contains("创建奖品失败");
    }

    /**
     * 仅输入奖品名称 + 奖品图片
     */
    public void createPrizeFail03() {
        driver.findElement(By.xpath("//*[@id=\"prizeImageUrl\"]")).sendKeys("E:\\code_java\\autotest\\BlogUIautoTest\\src\\test_image\\电动车.png");
        driver.findElement(By.xpath("/html/body/div/button")).click();
        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        assert actualMsg.contains("创建奖品失败");
    }

    /**
     * 奖品名称 + 图片 + 描述
     */
    public void createPrizeFail04() {
        driver.findElement(By.xpath("//*[@id=\"description\"]")).sendKeys("web自动化测试电动车");

        driver.findElement(By.xpath("/html/body/div/button")).click();
        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        assert actualMsg.contains("创建奖品失败");
    }

    /**
     * 正常添加奖品信息
     */
    public void createPrizeSuccess() {
        locationSubpage("/html/body/div[2]/div[1]/ul/li[2]/ul/li[2]/a");
        driver.findElement(By.xpath("//*[@id=\"prizeName\"]")).sendKeys("自动化测试奖品");
        driver.findElement(By.xpath("//*[@id=\"prizeImageUrl\"]")).sendKeys("E:\\code_java\\autotest\\BlogUIautoTest\\src\\test_image\\电动车.png");
        driver.findElement(By.xpath("//*[@id=\"description\"]")).sendKeys("web自动化测试电动车");
        driver.findElement(By.xpath("//*[@id=\"price\"]")).sendKeys("1999");
        driver.findElement(By.xpath("/html/body/div/button")).click();
        // 处理创建成功的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        String expectMsg = "奖品创建成功！";

        assert expectMsg.equals(actualMsg);
    }
}
